package com.txc.pay.alipayLife.utils;

import com.txc.common.utils.pay.ByteDataBuffer;
import com.txc.common.utils.pay.JsonToMap;

import java.util.Map;

/**
 * 解析报文
 *
 * @author tao
 */
public class DeSplitUtil {
    private static final byte BLOCK_START_SIGN = 0x68;
    private static final String ENCODING = "UTF-8";

    public static Map<String, Object> doCanProcess2Map(ByteDataBuffer obj) throws Exception {
        obj.setEncoding(ENCODING);
        obj.setInBigEndian(false);
        int totalLen = 0; // 长度
        byte sign = obj.readInt8();
        if (sign != BLOCK_START_SIGN) {
            throw new Exception("无法找到起始标记!");
        }
        totalLen = obj.readInt32();
        obj.readString(6);
        byte[] dataBytes = new byte[totalLen];
        obj.readBytes(dataBytes);
        String message = new String(dataBytes);
        // 报文是json格式，把json报文转换成Map类型的
        return JsonToMap.toMap(message);
    }

}
